(function(angular){
	'use strict';
	angular.module('fs.waf').controller('cda.changePassword.initController',initController);
	initController.$inject = ['$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'storageService', 'modalService'];
	function initController ($scope, Constants, layoutConfig, validatorConfig, gridService, authService, storageService, modalService){
		var ctrl = this, changePasswordInfo = $scope.$stateParams.params;
		
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.init = function (){
			ctrl.user = {};
			//var siteCode = storageService.getUserInfo().siteCode;\
			var companyCode = storageService.getUserInfo().workingCompanyCode;
			var userName = storageService.getUserInfo().userName;
            //ctrl.user = response.data || changePasswordInfo;
			ctrl.user.companyCode = companyCode;
			ctrl.user.userName = userName;
			
		};
		ctrl.reset=function(){
			ctrl.user.userPwd=null;
			ctrl.user.newPwd=null;
			ctrl.user.confirmPwd=null;
		};
		ctrl.changePassword = function() {
			ctrl.user.userPwd = ctrl.user.userPwd;
			ctrl.user.newPwd = ctrl.user.newPwd;
			ctrl.user.confirmPwd = ctrl.user.confirmPwd;
			var errorInfo = false;
			if ( !ctrl.user.userPwd ) {
				modalService.openErrorDialogs({
					'messages' : ['js.pub.error.old-password-required']
				});
				errorInfo = true;
				return errorInfo;
			}
			if ( !ctrl.user.newPwd ) {
				modalService.openErrorDialogs({
					'messages' : ['js.pub.error.new-password-required']
				});
				errorInfo = true;
				return errorInfo;
			}
			if ( !ctrl.user.confirmPwd) {
				modalService.openErrorDialogs({
					'messages' : ['js.pub.error.confirm-new-password-required']
				});
				errorInfo = true;
				return errorInfo;
			}
			if ( ctrl.user.userPwd != "" && ctrl.user.newPwd != "" && ctrl.user.userPwd == ctrl.user.newPwd) {
				modalService.openErrorDialogs({
					'messages' : ['js.pub.error.new-password-different']
				});
				errorInfo = true;
				return errorInfo;
			}
			if ( ctrl.user.confirmPwd != "" && ctrl.user.newPwd != "" && ctrl.user.confirmPwd != ctrl.user.newPwd) {
				modalService.openErrorDialogs({
					'messages' : ['js.pub.error.confirm-new-password-same']
				});
				errorInfo = true;
				return errorInfo;
			}
			if ( !errorInfo) {
				return authService.changePassword(ctrl.user).then(saveSuccessful, saveFailed);
			}
		};
		
		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.pwd.changed' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('waf.home', {});
					}
				}
			});
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}	
})(angular);